<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 1.链式编程
        // new Promise((resolve,reject) => {
        //     setTimeout(() => {
        //         resolve()
        //     },1000)
        // }).then(() => {
        //     console.log('First Hello World')
        //     console.log('First Hello World')
        //     console.log('First Hello World')
        //     console.log('First Hello World')
        //     console.log('First Hello World')

        //     return new Promise((resolve,reject) => {
        //         setTimeout(() => {
        //             resolve()
        //         },1000)
        //     })
        // }).then(() => {
        //     console.log('Second Hello World')
        //     console.log('Second Hello World')
        //     console.log('Second Hello World')
        //     console.log('Second Hello World')
        //     console.log('Second Hello World')

        //     return new Promise((resolve,reject) => {
        //         setTimeout(() => {
        //             resolve()
        //         },1000)
        //     })
        // }).then(() => {
        //     console.log('Third Hello World')
        //     console.log('Third Hello World')
        //     console.log('Third Hello World')
        //     console.log('Third Hello World')
        //     console.log('Third Hello World')
        // })

        // 2.Promise 写法1
        // new Promise((resolve,reject) => {
        //     setTimeout(() => {
        //         resolve('Hello Promise')
        //         // reject('error')
        //     },1000)
        // }).then((data) => {
        //     console.log(data)
        //     console.log(data)
        //     console.log(data)
        //     console.log(data)
        //     console.log(data)
        // }).catch((err) => {
        //     console.log(err)
        // })

        // 3.Promise 写法2
        // new Promise((resolve,reject) => {
        //     setTimeout(() => {
        //         // resolve('Hello Promise')
        //         reject('error')
        //     },1000)
        // }).then(data => {
        //     console.log(data)
        //     console.log(data)
        //     console.log(data)
        //     console.log(data)
        //     console.log(data)
        // },err => {
        //     console.log(err);
        // })

        // 4.Promise 写法3
        // new Promise((resolve,reject) => {
        //     setTimeout(() =>{
        //         resolve('aaa')
        //     },1000)
        // }).then(res => {
        //     console.log(res,'第一层的处理代码')

        //     return new Promise(resolve => {
        //         resolve(res + '111')
        //     })
        // }).then(res => {
        //     console.log(res,'第二层的处理代码')

        //     return Promise.resolve(res + '222')
        // }).then(res => {
        //     console.log(res,'第三层的处理代码')

        //     return res + '333'
        // }).then(res => {
        //     console.log(res,'第四层的处理代码')
        // })

        // 5.Promise 写法4
        new Promise((resolve,reject) => {
            setTimeout(() =>{
                resolve('aaa')
            },1000)
        }).then(res => {
            console.log(res,'第一层的处理代码')

            return new Promise(resolve => {
                resolve(res + '111')
            })
        }).then(res => {
            console.log(res,'第二层的处理代码')

            // return Promise.reject('error')
            throw 'error'
        }).then(res => {
            console.log(res,'第三层的处理代码')

            return res + '333'
        }).then(res => {
            console.log(res,'第四层的处理代码')
        }).catch(err => {
            console.log(err)
        })

        // 5.Promise all 处理多个请求
        Promise.all([
            new Promise((resolve,reject) => {
                setTimeout(() =>{
                    resolve({name:'mike',age:18})
                },2000)
            }),
            new Promise((resolve,reject) => {
                setTimeout(() =>{
                    resolve({name:'amy',age:19})
                },1000)
            })
        ]).then(results => {
            console.log(results)
        })
    </script>
</body>
</html>